#include "merchandise.h"
#include <string.h>
#include <stdio.h>
#ifdef OS_WIN
#include <conio.h>
#endif

static List_t merchandises;

merchandise_fun_t merchandise_funs[10] = {
    NULL,
    put_in,
    browse,
    sort,
    search,
    put_out,
    modify_info
};

int match_id(value_t val, void* pattern);
int match_name(value_t val, void* pattern);
int match_price(value_t val, void* pattern);
int match_type(value_t val, void* pattern);

static search_fun_t match_funcs[5] = {
    NULL,
    match_id,
    match_name,
    match_price,
    match_type
};

//match id
inline int match_id(value_t val, void* pattern)
{
    if (val.id == *(int*)pattern)
        return 0;
    return -1;
}
//match name
inline int match_name(value_t val, void* pattern)
{
    if (strncmp(val.name, (char*)pattern, strlen(val.name))==0)
        return 0;
    return -1;
}
//match price
inline int match_price(value_t val, void* pattern)
{
    if (val.price == *(float*)pattern)
        return 0;
    return -1;
}
//match type
inline int match_type(value_t val, void* pattern)
{
    if (val.type == *(int*)pattern) 
        return 0;
    return -1;
}

int put_in()
{
    merchandise_t new_merchandise;
    printf("number:");
    scanf("%d", &new_merchandise.id);
    printf("name:");
    scanf("%s", new_merchandise.name);
    // printf("price:");
    // scanf("%f", &new_merchandise.price);
    // printf("type:");
    // scanf("%d", &new_merchandise.type);
    if (list_add(&merchandises, new_merchandise) ==0 ){
        puts("put in merchandise succeed!");
        return 0;
    }
    else {
        perror("put in meschandise failed!");
        return -1;
    }
}

int browse()
{
    puts("商品信息列表");
    puts("--------------------------------------------------------------------");
    puts("| 编号 | 名称                                      |   价格  | 类别 |");
    puts("--------------------------------------------------------------------");
    merchandise_t mer;
    while (list_traverse(&merchandises, &mer, 1) == 0) {
        printf("| %04d | %-40s  | %7.2f | %04d |\n", mer.id, mer.name, mer.price, mer.type);
    }
    puts("--------------------------------------------------------------------");
    return 0;
}

int sort()
{
    int selection = 0;
    printf("取消:0, 按编号排序:1, 按价格排序:2\n>");
    do {
        scanf("%d", &selection);
    } while (selection < 0 || selection > 2);
    if (selection == 0) {
        puts("排序已经取消");
        return 0;
    }
    value_t temp_val;

    List_t* pt  = merchandises.next;
    List_t* pb  = pt;
    List_t* min = pt;
    while (pt != NULL) {
        pb = pt;
        while (pb != NULL) {
            min = selection==1?(pb->value.id < min->value.id?pb:min):(pb->value.price < min->value.price?pb:min);
            pb = pb->next;
        }
        temp_val = pt->value;
        pt->value = min->value;
        min->value = temp_val;
        pt = pt->next;
        min = pt;
    }
    return 0;
}

int search()
{
    int selection;
    int target_id;
    char target_name[200];
    printf("取消:0, 以编号搜索:1, 以名称搜索:2, \n>");
    do {
        scanf("%d", &selection);
    }while (selection<0 || selection > 2);
    merchandise_t mer;
    // if (list_search(&merchandises, search_funcs[selection], pattern, &mer) == 0 ) {

    // }
    switch (selection) {
    case 0:
        return 0;
        break;
    case 1:
        printf("输入编号:");
        scanf("%d", &target_id);
        puts("商品信息列表");
        puts("---------------------------------------------------------------------");
        puts("| 编号 | 名称                                       |   价格  | 类别 |");
        puts("---------------------------------------------------------------------");
        while (list_traverse(&merchandises, &mer, 1) == 0) {
            if (mer.id == target_id) {
                printf("| %04d | %-40s  | %7.2f | %04d |\n", mer.id, mer.name, mer.price, mer.type);
                break;
            }
        }
        puts("---------------------------------------------------------------------");
        break;
    case 2:
        printf("输入名称:");
        scanf("%s", target_name);
        puts("商品信息列表");
        puts("--------------------------------------------------------------------");
        puts("| 编号 | 名称                                      |   价格  | 类别 |");
        puts("--------------------------------------------------------------------");
        merchandise_t mer;
        while (list_traverse(&merchandises, &mer, 1) == 0) {
            if (strncmp(target_name, mer.name, sizeof(target_name))==0) {
                printf("| %04d | %-40s  | %7.2f | %04d |\n", mer.id, mer.name, mer.price, mer.type);
                break;
            }
        }
        puts("--------------------------------------------------------------------");
        break;
    default:
        break;
    }
    return 0;
    
}

int put_out()
{
    printf("输入出库商品编号:");
    int number;
    scanf("%d", &number);
    if (list_del(&merchandises, number) == 0) {
        puts("出库成功!");
        return 0;
    }
    else {
        puts("出库失败!");
        return -1;
    }
}

int modify_info()
{
    int mer_num;
    printf("输入要修改商品的编号:");
    scanf("%d", &mer_num);
    merchandise_t mer;
    char ssss[200];
    char selec;
    int index = list_search(&merchandises, match_funcs[1], (void*)&mer_num, &mer);
    if (index > 0) {
        puts("输入新的信息:");
        printf("新编号[%d]:", mer.id); scanf("%d", &mer.id);
        printf("新名称[%s]:", mer.name);  scanf("%s", mer.name);
        printf("新价格[%f]:", mer.price); scanf("%f", &mer.price);
        printf("新类型[%d]:", mer.type); scanf("%d", &mer.type);
        return list_update(&merchandises, index, mer);
    }
    return -1;
}
